package com.vr.xuecheng.learning.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vr.xuecheng.base.exception.XueChengPlusException;
import com.vr.xuecheng.common.util.SecurityUtils;
import com.vr.xuecheng.learning.dto.CourseTablesDTO;
import com.vr.xuecheng.learning.entity.ChooseCourse;
import com.vr.xuecheng.learning.entity.CourseTables;
import com.vr.xuecheng.learning.mapper.CourseTablesMapper;
import com.vr.xuecheng.learning.request.GetCourseTableRequestParams;
import com.vr.xuecheng.learning.response.GetCourseTableResponse;
import com.vr.xuecheng.learning.service.ICourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzh
 */
@Slf4j
@Service
public class CourseTablesServiceImpl extends ServiceImpl<CourseTablesMapper, CourseTables> implements ICourseTablesService {

    private final CourseTablesMapper courseTablesMapper;

    public CourseTablesServiceImpl(CourseTablesMapper courseTablesMapper) {
        this.courseTablesMapper = courseTablesMapper;
    }

    @Override
    public CourseTables addCourseTabls(ChooseCourse chooseCourse) {
        // 判断是否已经选过
        CourseTables queryResult = courseTablesMapper.selectOne(new LambdaQueryWrapper<CourseTables>()
                .eq(CourseTables::getCourseId, chooseCourse.getCourseId())
                .eq(CourseTables::getUserId, chooseCourse.getUserId())
        );
        if (queryResult!=null) {
            log.error("用户已经选过该课程,添加失败");
            return queryResult;
        }
        CourseTables courseTables = CourseTables.builder()
                .chooseCourseId(chooseCourse.getId())
                .userId(chooseCourse.getUserId())
                .courseId(chooseCourse.getCourseId())
                .companyId(chooseCourse.getCompanyId())
                .courseName(chooseCourse.getCourseName())
                .courseType(chooseCourse.getOrderType())
                .validtimeStart(chooseCourse.getValidtimeStart())
                .validtimeEnd(chooseCourse.getValidtimeEnd())
                .remarks(chooseCourse.getRemarks())
                .build();
        courseTablesMapper.insert(courseTables);
        return courseTables;
    }

    @Override
    public GetCourseTableResponse getCourseList(GetCourseTableRequestParams req) {
        if (req==null) {
            XueChengPlusException.cast("请求参数为空");
        }
        // 获取用户数据
        SecurityUtils.User user = SecurityUtils.getUser();
        if (user==null) {
            XueChengPlusException.cast("用户未登录,请重新登录");
        }
        String userId = user.getId();
        List<CourseTables> courseTablesList = courseTablesMapper.selectList(new LambdaQueryWrapper<CourseTables>()
                .eq(CourseTables::getUserId, userId)
                .eq(StrUtil.isNotBlank(req.getCourseType()),CourseTables::getCourseType, req.getCourseType())
                .func("2".equals(req.getExpiresType()) || "1".equals(req.getExpiresType()), wrapper -> {
                    String expiresType = req.getExpiresType();
                    LocalDateTime nowDate = LocalDateTime.now();
                    if ("2".equals(expiresType)) {
                        // 拿到过期的课程
                        // validtime_end小于当前时间记为过期
                        wrapper.lt(CourseTables::getValidtimeEnd, nowDate);
                    }
                    if ("1".equals(expiresType)) {
                        // 即将过期
                        // 把还剩一个星期内表示快过期了,包含一个星期,表示最多剩七天过期
                        wrapper.ge(CourseTables::getValidtimeEnd, nowDate)
                                .le(CourseTables::getValidtimeEnd, nowDate.plusDays(7));

                    }
                })
        );
        List<CourseTablesDTO> courseTablesDTOList = BeanUtil.copyToList(courseTablesList, CourseTablesDTO.class);
        GetCourseTableResponse response = new GetCourseTableResponse();
        response.setItems(courseTablesDTOList);
        return response ;
    }
}
